using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

[assembly: WebResource("SBPweb.Controls.Web.UpdateProgress.js", "text/javascript")]

namespace SBPweb.Controls.Web
{

	/// <summary>
	/// Provides visual feedback in the browser when the contents of one or more UpdatePanel controls are updated
	/// </summary>
	[ToolboxData("<{0}:UpdateProgress runat=server></{0}:UpdateProgress>")]
	public class UpdateProgress : System.Web.UI.UpdateProgress
	{

		#region Private members

		private string mCssClass = "divRelative";

		#endregion

		#region Public properties

		/// <summary>
		/// Gets or sets the css class of the control
		/// </summary>
		public string CssClass
		{
			get
			{
				return mCssClass;
			}
			set
			{
				mCssClass = value;
			}
		}

		#endregion

		#region Overridden methods

		/// <summary>
		/// Occurs after the Control object is loaded but prior to rendering.
		/// </summary>
		/// <param name="e">EventArgs e</param>
		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);

			UpdatePanel up = this.Parent.FindControl(this.AssociatedUpdatePanelID) as UpdatePanel;

			string script = string.Format("setProgressDivProperties('{0}', '{1}');", up.ClientID, this.ClientID);
			string script2 = string.Empty;
			script2 += "var postbackElement;\n";
			script2 += "Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(panelsProgessPageLoadedEventHandler);\n";
			script2 += "Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(panelsProgressBeginRequestHandler);\n";

			this.Page.ClientScript.RegisterClientScriptResource(this.GetType(), "SBPweb.Controls.Web.UpdateProgress.js");

			this.Page.ClientScript.RegisterStartupScript(this.GetType(), this.ID, script, true);
			this.Page.ClientScript.RegisterStartupScript(this.GetType(), "_AJAXHandlers", script2, true);
		}

		/// <summary>
		/// Sends server control content to a provided HtmlTextWriter object, which writes the content to be rendered on the client.
		/// </summary>
		/// <param name="writer">HtmlTextWriter writer</param>
		public override void RenderControl(HtmlTextWriter writer)
		{
			writer.AddAttribute("class", mCssClass);
			base.RenderControl(writer);
		}

		#endregion

	}
}